﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using KP_CommonLibrary.KP_ObjectReferences;

namespace KP_CommonLibrary.KP_DataAccess
{
    public class KP_DALBase : IDisposable, KP_IDAL
    {
        
        public static string TablePrefix { get; set; }
        public const string SelectDynamicProc = "[dbo].[kp_DataTableSelectDynamic]";
        protected bool innerConnection = false;
        protected bool innerTransaction = false;
        public SqlTransaction Transaction { get; set; }
        private SqlConnection mc_Connection;
        public SqlConnection Connection
        {
            get
            {
                if (mc_Connection.State == ConnectionState.Closed)
                    mc_Connection.Open();

                return mc_Connection;
            }
            set
            {
                CloseConnection();
                innerConnection = false;
                mc_Connection = value;
            }
        }

        public void OpenConnection()
        {
            try
            {
                innerConnection = true;
                innerTransaction = false;
                mc_Connection = new SqlConnection(KP_OCnnSql.Cnn_String);
                mc_Connection.Open();
            }
            catch (Exception ex)
            {
                innerConnection = false;
                throw ex;
            }
        }

        public void CloseConnection()
        {

            if (innerConnection)
            {
                if (Connection != null && Connection.State != ConnectionState.Closed)
                {
                    if (innerTransaction && Transaction != null)
                    {
                        Transaction.Commit();
                    }

                    Connection.Close();
                }
                innerConnection = false;
                innerTransaction = false;
            }
        }

        public void BeginTransaction()
        {
            if (innerConnection && Connection != null)
            {
                Transaction = Connection.BeginTransaction();
                innerTransaction = true;
            }
            else
            {
                if (!innerConnection)
                    throw new Exception("Not Owner of Connection");
                if (Connection == null)
                {
                    throw new Exception("Connection not references");
                }
            }

        }

        public string GetEntityName(string tableName)
        {
            if (tableName.StartsWith(TablePrefix))
            {
                return tableName.Substring(TablePrefix.Length);
            }
            return tableName;
        }
        public static DataTable GetSchema(string tableName)
        {
            SqlConnection conn = new SqlConnection(KP_OCnnSql.Cnn_String);
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("Select Top 1 * from " + tableName, conn);
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable tblSchema = new DataTable(tableName);
                adapter.FillSchema(tblSchema, SchemaType.Source);
                return tblSchema;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn.State != ConnectionState.Closed)
                    conn.Close();
            }
            //return SqlHelper.ExecuteDataTable(KP_OCnnSql.Cnn_String, CommandType.Text, "Select Top 0 from " + tableName);

        }

        static KP_DALBase()
        {
            TablePrefix = "tbl";
        }

        #region IDisposable Members

        public void Dispose()
        {
            try
            {
                CloseConnection();

            }
            catch
            {
            }
            finally
            {
                if (!innerConnection)
                    mc_Connection = null;
                if (!innerTransaction)
                    Transaction = null;
            }
        }

        #endregion
    }

    
}
